<?php

namespace Budget\BudgetBundle\Controller;

//use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Budget\BudgetBundle\Entity\Category;
use Budget\BudgetBundle\Form\CategoryType;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class CategoryController extends MainController
{
 
    public function indexAction() 
    {   
        $categories = $this->getEntityManager()->getRepository('BudgetBudgetBundle:Category')
                       ->getCategoriesForFamily($this->getUser()->getFamily());
        
        return $this->render('BudgetBudgetBundle:Category:index.html.twig', array(
            'categories' => $categories
        ));
    }
    
//    public function newAction(Request $request) 
//    {
//       $user       = $this->getUser();
//       $userFamily = $user->getFamily();
//       $category   = new Category();
//       $form       = $this->createForm(new CategoryType($userFamily), $category);
//       
//        if ($request->getMethod() == "POST") {
//            $form->bind($request);
//            if ($form->isValid()) {
//                $em = $this->getEntityManager();
//                $category->setFamily($userFamily);
//                $category->setCreated($user);
//                $em->persist($category);
//                $em->flush();
//                $this->setPositveMsg('Category added!');
//                return $this->redirect($this->generateUrl('BudgetBudgetBundle_categories_new'));
//            }  
//        }
//       
//        return $this->render('BudgetBudgetBundle:category:new.html.twig', array(
//            'form' => $form->createView()
//        ));          
//    }
    public function newAction(Request $request) 
    {
       $user     = $this->getUser();
       $idParent = $request->request->get('id');
       $name     = $request->request->get('name');
       $category = new Category();
       $response = new \stdClass;
       $em       = $this->getEntityManager();
       
       $response->status = true;
       
       if (!$name) {
           $response->status = false; 
           return new JsonResponse($response);
       }
       if ($idParent) {
            $parentCategory = $em->getRepository('BudgetBudgetBundle:Category')->findByIdForFamily($user->getFamily(), $idParent);

             if (!$parentCategory) {
                 $response->status = false; 
                 return new JsonResponse($response);
             } 
        } else {
            $parentCategory = null;
        }
        
        $category->setFamily($user->getFamily());
        $category->setCreated($user);
        $category->setParent($parentCategory);
        $category->setName($name);
        $em->persist($category);
        $em->flush();
      
        return new JsonResponse($response);         
    }
    
    public function deleteAction(Request $request) 
    {
        $id       = $request->request->get('id');
        $user     = $this->getUser();
        $em       = $this->getEntityManager();
        $category = $em->getRepository('BudgetBudgetBundle:Category')->findByIdForFamily($user->getFamily(), $id);
        
        if ($category) {
            $children = $em->getRepository('BudgetBudgetBundle:Category')->checkActiveChildren($category);
            $expenses = $em->getRepository('BudgetBudgetBundle:Expense')->checkActiveExpense($category);

            if ($children == 0 && $expenses == 0) {
                $category->setDateDeleted(new \DateTime());
                $category->setDeletedBy($user);
                $em->persist($category);
                $em->flush();
                return new Response('TRUE');
               // $this->setPositveMsg('Category was removed!');
            }
        }
        return new Response('FALSE');
        //return $this->redirect($this->generateUrl('BudgetBudgetBundle_categories'));
    }
    
    public function editAction(Request $request, $id) 
    {    
        $user       = $this->getUser();
        $userFamily = $user->getFamily();
        $em         = $this->getEntityManager();
        $category   = $em->getRepository('BudgetBudgetBundle:Category')->findByIdForFamily($userFamily, $id);
    
        if (!$category) {
            $this->setPositveMsg('Category not found!');    
            return $this->redirect($this->generateUrl('BudgetBudgetBundle_categories'));
        } 
       
        $form = $this->createForm(new CategoryType($userFamily, $category), $category);

        if ($request->getMethod() == "POST") {
            $form->bind($request);

            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $category->setFamily($userFamily);
                $em->persist($category);
                $em->flush();
                $this->setPositveMsg('Category saved!');
                return $this->redirect($this->generateUrl('BudgetBudgetBundle_categories'));
            }
        }

        return $this->render('BudgetBudgetBundle:Category:edit.html.twig', array(
            'form' => $form->createView(),
        ));
                
    }
    
    public function renameAction(Request $request) 
    {        
        $idCategory = $request->request->get('id');
        $newName    = $request->request->get('name');
        $user       = $this->getUser();
        $userFamily = $user->getFamily();
        $em         = $this->getEntityManager();
        $category   = $em->getRepository('BudgetBudgetBundle:Category')->findByIdForFamily($userFamily, $idCategory);
    
        if ($category) {
            $category->setName($newName);
            $em->persist($category);
            $em->flush();
            return new Response('TRUE');
        }
  
        return new Response('FALSE');
        
    }
    
}